package com.example.zbh.dsliao.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.zbh.dsliao.R;

/**
 * 自定义加载中的dialog
 */
public class CustomDialog extends Dialog {

    private static int default_width = 400;//默认宽度
    private static int default_height = 300;//默认高度
    private TextView message;
    private Context context;
    private String title;


    public CustomDialog(Context context, String title) {
        super(context, R.style.DialogTheme);
        this.context = context;
        this.title = title;
        init();
        //设置dialog属性点击外部dialog不会消失
        this.setCanceledOnTouchOutside(false);
    }

    private void init() {
        //加载布局
        setContentView(R.layout.dialog_layout);
        message = (TextView)findViewById(R.id.message);
        //设置显示文字  从初始化地方传过来的
        message.setText(title);
        //设置窗口属性
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        float density = getDensity(context);
        params.width = default_width;
        params.height = default_height;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);

    }

    /**
     * 获取显示密度
     * @param context
     * @return
     */
    public float getDensity(Context context) {
        Resources res = context.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        return dm.density;
    }


}
